package com.wdb.exchange.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.gson.Gson;
import com.wdb.exchange.constant.EventEnum;
import com.wdb.exchange.constant.SourceEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Event {

    @NonNull
    public EventEnum event;
    public Long userId;
    public String mobile;
    public String appId;
    public String openId;
    @NonNull
    public long eventTime;  //milliseconds
    @NotNull
    public SourceEnum source;
    @Size(max=500)
    public String extra;
    public Object payload;  // 额外的一些信息，可以是json

    public String toJson() {
        return new Gson().toJson(this);
    }

    public static Event fromJson(@NonNull String json) {
        return new Gson().fromJson(json, Event.class);
    }

//    public static void main(String[] args) {
//        Event event = new Event();
//        event.event = EventEnum.AWARD_DONE;
//        event.source = SourceEnum.API;
//        event.eventTime = (new Date()).getTime();
//
//        System.out.println(event.toJson());
//
//    }

}
